package cn.iocoder.yudao.module.wms.controller.admin.rdrecord11;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.wms.controller.admin.inventory.stock.vo.InventoryStockReqVO;
import cn.iocoder.yudao.module.wms.controller.admin.rdrecord11.vo.Rdrecord11PageReqVO;
import cn.iocoder.yudao.module.wms.controller.admin.rdrecord11.vo.Rdrecord11RespVO;
import cn.iocoder.yudao.module.wms.controller.admin.rdrecord11.vo.Rdrecord11SaveReqVO;
import cn.iocoder.yudao.module.wms.controller.admin.rdrecord11.vo.Rdrecords11RespVO;
import cn.iocoder.yudao.module.wms.dal.dataobject.rdrecord11.Rdrecord11DO;
import cn.iocoder.yudao.module.wms.dal.dataobject.rdrecord11.Rdrecords11DO;
import cn.iocoder.yudao.module.wms.service.inventory.stock.InventoryStockService;
import cn.iocoder.yudao.module.wms.service.rdrecord11.Rdrecord11Service;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 材料出库主")
@RestController
@RequestMapping("/wms/rdrecord11")
@Validated
public class Rdrecord11Controller {

    @Resource
    private Rdrecord11Service rdrecord11Service;

    @Resource
    private InventoryStockService inventoryStockService;

    @PostMapping("/create")
    @Operation(summary = "创建材料出库主")
    @PreAuthorize("@ss.hasPermission('wms:rdrecord11:create')")
    public CommonResult<Long> createRdrecord11(@Valid @RequestBody Rdrecord11SaveReqVO createReqVO) {
        return success(rdrecord11Service.createRdrecord11(createReqVO));
    }

    @GetMapping("/startProcess")
    @Operation(summary = "启动流程")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('wms:production-order:create')")
    public CommonResult<Boolean> startProcess(@RequestParam("id") Long id) {
        rdrecord11Service.process(id);
        return success(true);
    }

    @PutMapping("/update")
    @Operation(summary = "更新材料出库主")
    @PreAuthorize("@ss.hasPermission('wms:rdrecord11:update')")
    public CommonResult<Boolean> updateRdrecord11(@Valid @RequestBody Rdrecord11SaveReqVO updateReqVO) {
        rdrecord11Service.updateRdrecord11(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除材料出库主")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('wms:rdrecord11:delete')")
    public CommonResult<Boolean> deleteRdrecord11(@RequestParam("id") Long id) {
        rdrecord11Service.deleteRdrecord11(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得材料出库主")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('wms:rdrecord11:query')")
    public CommonResult<Rdrecord11RespVO> getRdrecord11(@RequestParam("id") String id) {
        Rdrecord11DO rdrecord11;
        try {
            rdrecord11 = rdrecord11Service.getRdrecord11(Long.valueOf(id));
        } catch (NumberFormatException e) {
            rdrecord11 = rdrecord11Service.getRdrecord11ByProcessInstanceId(id);
        }
        return success(BeanUtils.toBean(rdrecord11, Rdrecord11RespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得材料出库主分页")
    @PreAuthorize("@ss.hasPermission('wms:rdrecord11:query')")
    public CommonResult<PageResult<Rdrecord11RespVO>> getRdrecord11Page(@Valid Rdrecord11PageReqVO pageReqVO) {
        PageResult<Rdrecord11DO> pageResult = rdrecord11Service.getRdrecord11Page(pageReqVO);
        return success(BeanUtils.toBean(pageResult, Rdrecord11RespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出材料出库主 Excel")
    @PreAuthorize("@ss.hasPermission('wms:rdrecord11:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportRdrecord11Excel(@Valid Rdrecord11PageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<Rdrecord11DO> list = rdrecord11Service.getRdrecord11Page(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "材料出库主.xls", "数据", Rdrecord11RespVO.class,
                        BeanUtils.toBean(list, Rdrecord11RespVO.class));
    }

    // ==================== 子表（材料出库子） ====================

    @GetMapping("/rdrecords11/list-by-order-id")
    @Operation(summary = "获得材料出库子列表")
    @Parameter(name = "orderId", description = "订单id")
    @PreAuthorize("@ss.hasPermission('wms:rdrecord11:query')")
    public CommonResult<List<Rdrecords11RespVO>> getRdrecords11ListByOrderId(@RequestParam("orderId") Long orderId) {
        List<Rdrecords11DO> list = rdrecord11Service.getRdrecords11ListByOrderId(orderId);
        Rdrecord11DO rdrecord11 = rdrecord11Service.getRdrecord11(orderId);
        List<Rdrecords11RespVO> rdrecords11Resp = BeanUtils.toBean(list, Rdrecords11RespVO.class);
        rdrecords11Resp.forEach(rdrecords11RespVO -> {
            InventoryStockReqVO stockReqVO = new InventoryStockReqVO();
            stockReqVO.setInventoryId(rdrecords11RespVO.getInventoryId());
            stockReqVO.setWarehouseId(rdrecord11.getWarehouseId());
            stockReqVO.setLocationId(rdrecords11RespVO.getLocationId());
            stockReqVO.setBatchCode(rdrecords11RespVO.getBatch());
            rdrecords11RespVO.setAvailableAmount(inventoryStockService.getInventoryAvailableAmount(stockReqVO));
        });
        return success(rdrecords11Resp);
    }

}